<?php

include('xml_parser.phps');

class WebcpAPI {
	var $username='admin@test.com', $password='admin',
		$server='webcp.gyrbo.be', $port='81';
		
	var $data=array(), $error='', $error_level=0, $webcp_tag='';

	
	function send($q='') {
		$s = fsockopen($this->server, $this->port, $errno, $errstr, 30);
		if(!s) {
			$this->error_level = 4;
			$this->error = "$errstr ($errno)";
			$this->data = array();
			return;
		}
		
		fwrite($s, "GET /ext.php?$q HTTP/1.0\r\n"
			."Client: web-cp API Client\r\n"
			."\r\n");
		
		$this->error_level = 0;
		$this->error = '';
		$this->data = array();

		while(!feof($s)) {
			$data = trim(fgets($s, 1024));
			if(empty($data)) {
				break;	
			}
			
			list($name, $value) = explode(': ', $data, 2);
			if(trim($name) == 'X-Error') {
				list($this->error_level, $this->error) = explode(';', $value, 2);
			}
		}
		
		$block = 0;
		$data = '';
		while(!feof($s)) {
			$data .= /*trim(*/fgets($s, 1024)/*)*/;
		}		
		fclose($s);
		
		//if($this->error_level==0)
			echo '<xmp>'.$data.'</xmp><br />';
		
		$xmlparser = new XMLParser($data);
		$xmlparser->Parse();
		/*echo '<pre>';
		var_dump($xmlparser->document);
		echo '</pre>';*/
		$this->data = &$xmlparser->document;
	}
	
	function login($salt=NULL) {
		$this->webcp_tag = '';
		
		// get salt
		$this->error_level = 1;
		$this->error = '';
		for($count = 0;
		  $count <= 3 && $this->error_level == 1 && empty($this->data->salt[0]->tagData);
		  $count++) {
		  
			$this->send();
		}		
		if(empty($this->data->salt[0]->tagData)) {
			$this->error_level = 4;
			$this->error = 'no salt';
			return false;
		}
			
		// login
		$this->error_level = 1;
		$this->error = '';
		
		/*echo '<pre>';
		var_dump($this->data);
		echo '</pre>';*/
		
		for($count = 0; $count <= 3 && $this->error_level == 1
		  && empty($this->data->tag[0]->tagData)
		  && !empty($this->data->salt[0]->tagData);
		  $count++) {
		  
			$this->send('username='.$this->username
				.'&md5pass='.md5($this->password.$this->data->salt[0]->tagData));
		}
		
		if(empty($this->data->tag[0]->tagData)) {
			$this->error_level = 4;
			$this->error = 'no webcp_tag';
			return false;
		}
			
		$this->webcp_tag = $this->data->tag[0]->tagData;
		
		return true;
	}
	
	function call($action, $data=array()) {
		if(empty($this->webcp_tag)) {
			if(!$this->login())
				return false;
		}
			
		/*$replace = array(
			'&' => rawurlencode('&'),
			"\r" => rawurlencode("\r"),
			"\n" => rawurlencode("\n"),
			"%" => rawurlencode("%"),
			"=" => rawurlencode("=")
		);*/
		
		$q = 'webcp_tag='.$this->webcp_tag.'&action='.$action.'&';
		
		if(is_array($data)) {
			foreach($data as $k=>$v) {
				if(is_object($k) || is_array($k))
					continue;
			
				$q .= urlencode($k).'='.urlencode($v).'&';
			}
		}
		$q = substr($q, 0, -1); //strip last & for prettiness
		
		echo 'Q: '.$q.'<br />';
		
		for($count = 0; $count <= 3; $count++) {
			$this->send($q);
			
			if($this->error_level == 1) {
				if(!$this->login()) {
					echo 'Refresh login failed.';
					break;
				}
			} else
				break;
		}
		
		if($this->error_level == 0)		
			return true;
			
		return false;
	}
};

set_time_limit(180);

$api = new WebcpAPI;


$domaindata = array('ip_addr' => '192.168.253.128', 'hostname' => 'www',
	'domain' => 'newtest.com', 'aliases' => '', 'catchall' => 'false',
	'nohost' => 'true', 'redirect' => 'false', 'email' => 'me@my.com',
	'users' => 1, 'hd' => 20, 'subdomains' => 0, 'pointers' => 0,
	'transfer' => 1, 'databases' => 1, 'serverside[php]' => 'on');

if(!$api->call('reseller:newdomain', $domaindata)) {
	echo 'Error ('.$api->error_level.'): '.$api->error."<br />\n";
	return;
}

// Make sure the domain has been created
sleep(2);

$userdata = array('name' => 'Test User', 'username' => 'testy2',
	'password' => 'pass123', 'root' => '/', 'type' => 'standard',
	'level' => 3, 'hd' => 10, 'aliases' => '', 'catchall' => 'false',
	'shell' => 'off', 'autoreply' => '', 'forward' => '', 'lang' => 'english',
	'skin' => 'metroweb');

//fetch domain id
$userdata['number'] = $api->data->id[0]->tagData;

if(!$api->call('domain:newuser', $userdata)) {
	echo 'Error ('.$api->error_level.'): '.$api->error."<br />\n";
}

?>